with Chaines;use Chaines;
with Ada.Text_Io; use Ada.Text_Io;
package body Chaines is

   function Concat(S1,S2 : string) return String is
      Res : String(1..S1'Length+S2'Length);
   begin
      Res(Res'First..S1'Length) := S1;
      Res(S1'Length+1..res'Last):= S2;
      return Res;
   end Concat;

   -----------------------------------------

   function Lire_Ligne return String is
   begin
      if End_Of_Line then
         return "";
      else
         declare
            C: Character;
         begin
            Get(C);
            return Concat( (1 => C) ,Lire_Ligne);
         end;
      end if;
   end Lire_Ligne;

   -------------------

   procedure Lire_Ligne(S : in out String) is

   begin
      if End_Of_Line then
         null;
      else
         declare
            C:Character;
         begin
            Get(C);
            S := Concat(S,(1=>C));
            Lire_Ligne(S);
         end;
      end if;
   end Lire_Ligne;

   -----------------------------------------
 function Aux (Res: String) return String is
      begin
         if End_Of_Line then
            return res;
         else
            declare
               C:Character;
            begin
               Get(C);
               return Aux(Concat((1=>C),Res));
            end;
         end if;
      end Aux;

   function Lire_Ligne_Terminal return String is

   begin
     return Aux("");
   end Lire_Ligne_Terminal;

   ----------------------------------------------
   procedure Exchange(C:in out Chaine; X,Y: Positive) is
         Aux: Character := C(X);
      begin
         C(X) := C(Y);
         C(Y) := Aux;
      end Exchange;
      function RechercheIndexMin(C: Chaine; Debut : Positive) return Positive is
         Imin: Positive := Debut;
      begin
         for I in Debut .. C'Last loop
            if C(I) < C(Imin) then
               Imin := I;
            end if;
         end loop;
         return Imin;
      end RechercheIndexMin;

   function Tri(S :String) return String is

      Index : Positive;
      C: Chaine := Chainer(S);
   begin
      for I in C'First..C'Last loop
         Index := RechercheIndexMin(C,I);
         Exchange(C,I,Index);
      end loop;
      return Dechainer(C);
   end Tri;
   -----------------------------------------------

   function Chainer( S: String) return Chaine is
   begin
      return Chaine(S);
   end Chainer;

   -----------------------------------------------

   function Dechainer( C: Chaine) return String is
   begin
      return String(C);
   end Dechainer;

   -----------------------------------------------

   procedure Concat(S1,S2 : in String; Res : in out String) is
   begin
      if Res'Length = (S1'Length+S2'Length) then
        raise Constraint_Error;
      else
         Res(Res'First..S1'Length) := S1;
         Res(S1'Length+1..res'Last):= S2;
      end if;
   end Concat;
   end Chaines;


